<!--
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-08-19 11:30:52
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-02-24 10:24:31
-->
<template>
  <div class="component_container">
    <!-- 查询表单 -->
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-select
            v-model="formInline.code"
            placeholder="请选择页面编码"
            style="float: left"
          >
            <el-option
              v-for="(item, ind) in pageCodeList"
              :label="item.label"
              :value="item.code"
              :key="item.code + ind"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="formInline.nam"
            placeholder="请选择页面名称"
            style="float: left"
          >
            <el-option
              v-for="(item, ind) in pageNameList"
              :label="item.label"
              :value="item.code"
              :key="item.code + ind"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="formInline.data_table"
            placeholder="请选择数据表"
            style="float: left"
          >
            <el-option
              v-for="(item, ind) in dataTableList"
              :label="item.label"
              :value="item.code"
              :key="item.code + ind"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.content"
            placeholder="请输入页面ID"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addPage">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 查询结果列表显示 -->
    <el-table :data="tableData" style="width: 100%" ref="pageTable">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="页面ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="页面编码">
              <span v-if="!props.row.edit">{{ props.row.code }}</span>
              <el-input v-else v-model="props.row.code"></el-input>
            </el-form-item>
            <el-form-item label="页面名称">
              <span v-if="!props.row.edit">{{ props.row.name }}</span>
              <el-input v-else v-model="props.row.name"></el-input>
            </el-form-item>
            <el-form-item label="页面描述">
              <span v-if="!props.row.edit">{{ props.row.description }}</span>
              <el-input v-else v-model="props.row.description"></el-input>
            </el-form-item>
            <el-form-item label="最后修改时间">
              <span>{{ props.row.last_modified_time }}</span>
            </el-form-item>
            <el-form-item label="页面排序值">
              <span v-if="!props.row.edit">{{ props.row.sort_number }}</span>
              <el-input v-else v-model="props.row.sort_number"></el-input>
            </el-form-item>
            <el-form-item label="页面数据表">
              <span v-if="!props.row.edit">{{ props.row.data_table }}</span>
              <el-select
                v-else
                placeholder="请选择页面数据表"
                style="float: left"
                v-model="props.row.data_table"
              >
                <el-option
                  v-for="item in dataTableList"
                  :label="item.label"
                  :value="item.code"
                  :key="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="最后修改人">
              <span>{{ props.row.last_modified_author }}</span>
            </el-form-item>
            <el-form-item label="页面组件">
              <span v-if="!props.row.edit">
                <el-button type="text" @click="showDetail('component')"
                  >查看组件</el-button
                ></span
              >
              <el-drag-select
                v-else
                v-model="props.row.page_component"
                multiple
                filterable
                placeholder="请选择组件"
                ref="componentSelect"
                clearable
              >
                <el-option
                  v-for="(item, ind) in componentList"
                  :value="item.code"
                  :key="item.code + ind"
                  :label="item.label"
                ></el-option>
              </el-drag-select>
            </el-form-item>
            <el-form-item label="页面数据视图">
              <span v-if="!props.row.edit"
                ><el-button type="text" @click="showDetail('dataView')"
                  >查看数据视图</el-button
                ></span
              >
              <el-drag-select
                v-else
                v-model="props.row.page_data_view"
                multiple
                filterable
                placeholder="请选择"
                ref="dataViewSelect"
                clearable
              >
                <el-option
                  v-for="(item, ind) in dataViewList"
                  :value="item.code"
                  :key="item.code + ind"
                  :label="item.label"
                ></el-option>
              </el-drag-select>
            </el-form-item>
            <el-form-item label="页面请求连接">
              <span v-if="!props.row.edit"
                ><el-button type="text" @click="showDetail('request')"
                  >查看请求接口</el-button
                ></span
              >
              <el-drag-select
                v-else
                v-model="props.row.page_request"
                multiple
                filterable
                placeholder="请选择"
                ref="requestSelect"
                clearable
              >
                <el-option
                  v-for="(item, ind) in requestList"
                  :value="item.code"
                  :key="item.code + ind"
                  :label="item.label"
                ></el-option>
              </el-drag-select>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="页面编码" prop="code"> </el-table-column>
      <el-table-column label="页面名称" prop="name"> </el-table-column>
      <el-table-column label="数据视图描述" prop="description">
      </el-table-column>
      <el-table-column label="操作" prop="operate">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.edit"
            type="default"
            @click="saveEditPage(scope.row)"
            >保存</el-button
          >
          <el-button
            v-if="scope.row.edit"
            type="default"
            @click="cancelEditPage(scope.row)"
            >取消</el-button
          >
          <el-button
            v-if="!scope.row.edit"
            type="primary"
            @click="editPage(scope.row)"
            >编辑</el-button
          >
          <el-button type="primary" @click="delPage(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增页面弹出框 -->
    <el-dialog
      title="新增页面"
      :visible.sync="addPageDialogVisible"
      style="height: 650px"
    >
      <el-form
        :model="addPageForm"
        label-position="left"
        label-width="120px"
        class="demo-ruleForm"
        status-icon
        :rules="addRules"
        ref="addForm"
        size="medium"
      >
        <el-form-item label="页面编码">
          <el-input
            v-model="addPageForm.code"
            placeholder="请输入页面编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="页面名称">
          <el-input
            v-model="addPageForm.name"
            placeholder="请输入页面名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="页面描述">
          <el-input
            v-model="addPageForm.description"
            placeholder="请输入页面描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="页面排序">
          <el-input
            v-model="addPageForm.sort_number"
            placeholder="请输入页面排序"
          ></el-input>
        </el-form-item>
        <el-form-item label="页面数据表">
          <el-input
            v-model="addPageForm.data_table"
            placeholder="请输入页面数据表"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addPageDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd('addForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import ElDragSelect from "../../components/dragSelect/index.vue";
export default {
  name: "managePage",
  components: {
    ElDragSelect,
  },
  data() {
    return {
      componentList: [],
      dataViewList: [],
      requestList: [],
      addPageDialogVisible: false,
      formInline: {
        code: "",
        description: "",
        name: "",
        data_table: "",
      },
      addPageForm: {
        code: "",
        name: "",
        description: "",
        component_code: "",
        sort_number: "",
        data_table: "",
      },
      tableData: [],
      addRules: {},
      dataTableList: [],
      pageCodeList: [],
      pageNameList: [],
      componentObj: {},
      dataViewObj: {},
      requestObj: {},
    };
  },
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.getNameCodeTableList();
      this.getDataViewList();
      this.getRequestList();
      this.getComponentList();
      this.getPageList();
    });
  },
  methods: {
    getComponentList() {
      this.$http.component
        .getList(this.$address.baseUrl, {})
        .then((res) => {
          if (res.status) {
            this.formatOptionsList(res.data, "component");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getDataViewList() {
      this.$http.dataView
        .getList(this.$address.baseUrl, {})
        .then((res) => {
          if (res.status) {
            this.formatOptionsList(res.data, "dataView");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getRequestList() {
      this.$http.requestConfig
        .getList(this.$address.baseUrl, {})
        .then((res) => {
          if (res.status) {
            this.formatOptionsList(res.data, "request");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    formatOptionsList(dt, type) {
      if (type == "component") {
        this.componentList = [];
        dt.forEach((item) => {
          let tmp = {
            code: item.id,
            label: item.description + "(" + item.code + ")",
          };
          this[type + "Obj"][tmp.code] = tmp;
          this.componentList.push(tmp);
        });
      } else if (type == "dataView") {
        this.dataViewList = [];
        dt.forEach((item) => {
          let tmp = {
            code: item.id,
            label: item.description + "(" + item.code + ")",
          };
          this[type + "Obj"][tmp.code] = tmp;
          this.dataViewList.push(tmp);
        });
      } else if (type == "request") {
        this.requestList = [];
        dt.forEach((item) => {
          let tmp = {
            code: item.id,
            label: item.description + "(" + item.code + ")",
          };
          this[type + "Obj"][tmp.code] = tmp;
          this.requestList.push(tmp);
        });
      }
    },
    showDetail(type) {},
    addPage() {
      this.addPageDialogVisible = true;
    },
    getNameCodeTableList() {
      this.$http.page
        .getList(this.$address.baseUrl, {})
        .then((res) => {
          if (res.status) {
            this.dataTableList = [];
            this.pageNameList = [];
            this.pageCodeList = [];
            res.data.forEach((item) => {
              this.dataTableList.push({
                code: item.data_table,
                label: item.data_table,
              });
              this.pageCodeList.push({
                code: item.code,
                label: item.code,
              });
              this.pageNameList.push({
                code: item.name,
                label: item.name,
              });
            });
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getPageList() {
      this.$http.page
        .getList(this.$address.baseUrl, { ...this.formInline })
        .then((res) => {
          if (res.status) {
            this.tableData = res.data.map((item) => {
              let dvArr = item.page_data_view
                ? item.page_data_view.split(",")
                : [];
              item.page_data_view = dvArr.map((it, index) => {
                return it + ";" + index;
              });
              let cpArr = item.page_component
                ? item.page_component.split(",")
                : [];
              item.page_component = cpArr.map((it, index) => {
                return it + ";" + index;
              });
              let rpArr = item.page_request ? item.page_request.split(",") : [];
              item.page_request = rpArr.map((it, index) => {
                return it + ";" + index;
              });
              return item;
            });
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    confirmAdd(formName) {
      this.addPageDialogVisible = false;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            create_author: this.$store.state.user.userInfo.login_name,
            ...this.addPageForm,
          };
          this.$http.page
            .add(this.$address.baseUrl, params)
            .then((res) => {
              if (res.status) {
                this.getPageList();
              }
            })
            .catch((error) => {
              console.log(error);
            });
        }
      });
    },
    search() {
      this.getPageList();
    },
    saveEditPage(row) {
      let params = {
        ...row,
      };
      this.$delete(params, "edit");
      this.$delete(params, "page_component_arr");
      this.$delete(params, "page_data_view_arr");
      this.$delete(params, "page_request_arr");
      params.last_modified_author = this.$store.state.user.userInfo.login_name;
      let tdv = params.page_data_view.map((item) => {
        let tmp = item.split(";");
        return tmp.length >= 2 ? tmp[1] : tmp[0];
      });
      params.page_data_view = tdv.join(",");
      let tcp = params.page_component.map((item) => {
        let tmp = item.split(";");
        return tmp.length >= 2 ? tmp[1] : tmp[0];
      });
      params.page_component = tcp.join(",");
      let trq = params.page_request.map((item) => {
        let tmp = item.split(";");
        return tmp.length >= 2 ? tmp[1] : tmp[0];
      });
      params.page_request = trq.join(",");
      this.$http.page
        .edit(this.$address.baseUrl, params)
        .then((res) => {
          if (res.status) {
            this.getPageList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    cancelEditPage(row) {
      this.$set(row, "edit", false);

      this.$nextTick(() => {
        this.$refs.pageTable.toggleRowExpansion(row, false);
      });
    },
    editPage(row) {
      this.$set(row, "edit", true);
      row.page_data_view = row.page_data_view.map((item) => {
        let key = item.split(";")[0];
        return this.dataViewObj[key].label + ";" + item;
      });
      row.page_component = row.page_component.map((item) => {
        let key = item.split(";")[0];
        return this.componentObj[key].label + ";" + item;
      });
      row.page_request = row.page_request.map((item) => {
        let key = item.split(";")[0];
        return this.requestObj[key].label + ";" + item;
      });
      this.$nextTick(() => {
        this.$refs.pageTable.toggleRowExpansion(row, true);
      });
    },
    delPage(row) {
      this.$http.page
        .delete(this.$address.baseUrl, { id: row.id })
        .then((res) => {
          if (res.status) {
            this.getPageList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
<style scoped>
.el-form-item--small.el-form-item {
  float: left;
}
.demo-table-expand div {
  width: 45%;
  margin-left: 40px;
  margin-bottom: 0px;
}
.demo-table-expand div .contentArea {
  width: calc(100% - 200px);
}
</style>